package net.ubinow.mflex.data;

import net.ubinow.mflex.util.Constants;


public class IntQueue {
    
    private int _size = 0;
    private int[] _queue = null;
    private int _first = 0;
    private int _next = 0;
    
    /** Creates a new instance of Queue */
    public IntQueue( int size ) {
        _size = size;
        
        initQueue();
    }
    
    private void initQueue(){
        _queue = new int[_size];
    }
    
    public boolean isEmply(){
        return _first == _next;
    }
    
    /**
     * the item is added to the end of the queue, if the queue is full then the queue will be 
     * dequeued and this value will be returned otherwise -1 will be returned 
     * @return: n if the queue is full otherwise -1 
     **/
    public int queue( int item ){
        _queue[_next] = item;
        _next++;
        
        if( _next >= _size-1 )
            _next = 0;
        
        if( _next == _first ){
            // queue is full therefore we will move the first pointer forward 1 and return its value
            int res = _queue[_first];
            _first++;
            
            return res;
        } else{
            return Constants.NULL_INT;
        }
    }
    
    /**
     * dequeues the queue ie returns the first item in the queue
     * return: -1 if there are no items in the queue else the first value in the queue
     **/
    public int dequeue(){        
        if( _first == _next )
            return Constants.NULL_INT;
        
        int res = _queue[_first];
        _first++;        
        
        // bounds checking 
        if( _first >= _size-1 )
            _first = 0;        
        
        return res; 
    }
    
    /**
     * used to peek at the next value without actually taking it off the queue 
     **/
    public int peek(){
        if( isEmply() )
            return Constants.NULL_INT;
        
        return _queue[_first];
    }
}
